package com.platform.tony.dto;

import com.platform.tony.entity.Role;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
public class UserDto implements UserDetails, Serializable {

    private static final long serialVersionUID = 6598371682580490790L;

    // 主键
    private Long id;

    // 用户名
    private String username;

    // 密码
    private String password;

    // 禁用 启用
    private Boolean enabled;

    // account is valid non-expired
    private Boolean accountNonExpired;

    // not locked
    private Boolean accountNonLocked;

    // credentials are valid
    private Boolean credentialsNonExpired;

    // 角色 role
    private List<Role> roles = new ArrayList<>();

    // ? extends
    @Override
    public Collection<GrantedAuthority> getAuthorities() {

        List<GrantedAuthority> authorities = new ArrayList<>();
        roles.forEach(role -> authorities.add(new SimpleGrantedAuthority(role.getName())));
        return authorities;

    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
